package com.lanou.tipsy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.lanou.tipsy.R;
import com.lanou.tipsy.fragment.guide.GuidePageFragment;
import com.lanou.tipsy.tools.custom.CirclePageIndicator;
import com.lanou.tipsy.tools.custom.ZoomOutPageTransformer;

import java.text.SimpleDateFormat;


/**
 * Created by dllo on 15/10/9.
 *
 *   引导图Fragment
 *
 */
public class AndyViewPagerActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {
    public static final String TAG = "AndyViewPagerActivity";

    /**
     * The number of pages (wizard steps) to show in this demo.
     */
    private static final int NUM_PAGES = 4;
    private boolean misScrolled = false;
    /**
     * The pager widget, which handles animation and allows swiping horizontally
     * to access previous and next wizard steps.
     *
     *  pager组件 用于控制动画  并允许通过水平滑动的方式进行前一步操作或后一步操作
     */
    private ViewPager mPager;
    private CirclePageIndicator mPageIndicator;
    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;

    private String date, dateJudge;
    private SimpleDateFormat sDateFormat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_viewpager);
        // Instantiate a ViewPager and a PagerAdapter.
        //各种初始化
        mPager = (ViewPager) findViewById(R.id.pager);
        mPageIndicator = (CirclePageIndicator) findViewById(R.id.indicator);

        mPager.addOnPageChangeListener(this);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        mPageIndicator.setViewPager(mPager);
        mPager.setPageTransformer(true, new ZoomOutPageTransformer());
        // the other transformer
        //mPager.setPageTransformer(true, new DepthPageTransformer());
        dateJudge = "20151010";
        sDateFormat = new SimpleDateFormat("yyyyMMdd");
        date = sDateFormat.format(new java.util.Date());
    }

    @Override
    public void onBackPressed() {
        if (mPager.getCurrentItem() == 0) {
            // If the user is currently looking at the first step, allow the
            // system to handle the
            // Back button. This calls finish() on this activity and pops the
            // back stack.
            //第一次进入界面位于第一屏时允许使用后退键 此时结束这个activity并回退
            super.onBackPressed();
        } else {
            // Otherwise, select the previous step.
            //否则的话执行前一步操作
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }


    @Override
    public void onPageScrollStateChanged(int state) {
        switch (state) {
            case ViewPager.SCROLL_STATE_DRAGGING:
                Log.i("test", TAG + "SCROLL_STATE_DRAGGING");
                misScrolled = false;
                break;
            case ViewPager.SCROLL_STATE_SETTLING:
                Log.i("test",TAG + "SCROLL_STATE_SETTLING");
                misScrolled = true;
                break;
            case ViewPager.SCROLL_STATE_IDLE:
                Log.i("test", TAG +  "SCROLL_STATE_IDLE");
                if (mPager.getCurrentItem() == mPager.getAdapter().getCount() - 1 && !misScrolled) {

                    if (date.equals(dateJudge)) {
                        Intent intent = new Intent(AndyViewPagerActivity.this, EggsActivity.class);
                        startActivity(intent);
                        finish();
                    }else {
                        Intent intent = new Intent(AndyViewPagerActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
                misScrolled = true;
                break;
        }
    }
    /**
     * A simple pager adapter that represents 5 ScreenSlidePageFragment objects,
     * in
     * sequence.
     */

    private class ScreenSlidePagerAdapter extends FragmentPagerAdapter {

        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return GuidePageFragment.newInstance(position);
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }

    }

}